package com.murphy.algorithm.demo;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/10/19 09:56
 */
public class GenOne {

    public static void main(String[] args) {
        GenOne genOne = new GenOne();
        List<String> list = genOne.genP(3);
        System.out.println(JSON.toJSON(list));
    }

    public List<String> genP(int n){
        List<String> list = new ArrayList<>();
        gen("",list,n,n);
        return list;
    }
    public void gen(String result, List<String> list, int l, int r) {
        //如果左右括号都用完
        if (l == 0 && r == 0){
            list.add(result);
            return;
        }
        //如果左边还有，第一次一定是左边
        if (l > 0){
            gen(result+"<",list,l-1,r);
        }
        //如果右边大于左边才能生成右边
        if (l < r){
            gen(result+">",list,l,r-1);
        }
    }

}
